package com.ruoyi.app.common

import cn.hutool.core.util.RandomUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.ruoyi.back.mapper.GlobalParamDetailMapper
import com.ruoyi.back.v1.domain.GlobalParamDetail
import com.ruoyi.oss.client.IOSSClient
import com.ruoyi.oss.client.OSSClientFactory
import com.ruoyi.oss.config.OssConfig
import com.volcengine.service.visual.IVisualService
import com.volcengine.service.visual.impl.VisualServiceImpl
import com.volcengine.service.visual.model.request.VisualFaceFusionMovieGetResultRequest
import com.volcengine.service.visual.model.request.VisualFaceFusionMovieSubmitTaskRequest
import com.volcengine.service.visual.model.request.VisualFaceSwapRequest
import com.volcengine.service.visual.model.response.VisualFaceFusionMovieGetResultResponse
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*


data class PicChangeFaceParam(

    var check: Boolean? = false,

    var templateUrl: String? = null,

    var faceUrl: String? = null,
)


data class VideoChangeFaceParam(

    var videoUrl: String? = null,

    var faceUrl: String? = null,

    var logo: Boolean? = false,
)

const val APPID = "AKLTNWMyNjQyN2YzZjVjNGQzMjlkMjhhYzVkMjJmNGRkMmU"
const val SECRET = "WXpWaE9HWTBNV1EyTmpsaU5EbGlNbUkwTjJNMVpUZ3daVEF6WlRnM1ptWQ=="

fun instance(): IVisualService = VisualServiceImpl.getInstance().apply {
    accessKey = APPID
    secretKey = SECRET
}

@Service
class HuoshanApi {


    @Autowired
    private lateinit var ossConfig: OssConfig


    fun uploadBase64(imageBase64: String): String {

        val ossClient: IOSSClient = OSSClientFactory.build("aliyun")
        ossClient.init(ossConfig)
        return ossClient.upload(imageBase64.base64ToInputStream(), "${RandomUtil.randomNumbers(6)}.png")
    }

    fun String.base64ToInputStream() = Base64.getDecoder().decode(this).inputStream()


    fun videoSwapResult(taskId: String): VisualFaceFusionMovieGetResultResponse =
        instance().faceFusionMovieGetResult(VisualFaceFusionMovieGetResultRequest().apply {
            this.taskId = taskId
            this.reqKey = "facefusionmovie_usage"
        })

    fun videoSwapFace(videoChangeFaceParam: VideoChangeFaceParam): String =
        instance().faceFusionMovieSubmitTask(VisualFaceFusionMovieSubmitTaskRequest().apply {
            reqKey = "facefusionmovie_usage"
            imageUrl = videoChangeFaceParam.faceUrl
            videoUrl = videoChangeFaceParam.videoUrl
            this.enableFaceBeautify = true
        }).data.taskId

    fun picSwapFace(picChangeFaceParam: PicChangeFaceParam) =
        instance().let {
            it.faceSwap(VisualFaceSwapRequest().apply {
                imageBase64 = ""
                templateBase64 = ""
                imageUrl = picChangeFaceParam.faceUrl
                templateUrl = picChangeFaceParam.templateUrl
                actionId = "faceswap"
                version = "2.0"
                doRisk = picChangeFaceParam.check
            }).let {
                uploadBase64(it.data.image)
            }
        }
}


